package com.qipay.admin.home.controller;

import com.qipay.admin.BaseController;
import com.qipay.admin.tags.PageModel;
import com.qipay.baas.model.CpAccountTradlog;
import com.qipay.baas.model.Order;
import com.qipay.baas.model.SpUser;
import com.qipay.mapper.CpAccountTradlogMapper;
import com.qipay.mapper.OrderMapper;
import com.qipay.mapper.SpUserMapper;
import com.qipay.security.UserDetailsServiceImpl;
import com.qipay.user.entity.UserLevel;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.annotation.AuthenticationPrincipal;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.ModelAndView;

import java.io.UnsupportedEncodingException;
import java.net.URLEncoder;
import java.text.SimpleDateFormat;
import java.util.*;

@Controller
public class HomeController extends BaseController {

    @Autowired
    CpAccountTradlogMapper cpAccountTradlogMapper;
    @Autowired
    OrderMapper orderMapper;
    @Autowired
    SpUserMapper spUserMapper;

    private final static SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
    @RequestMapping(value = {"","/", "/index.jsp", "/index","/list"}, method = RequestMethod.GET)
    public ModelAndView index(@AuthenticationPrincipal UserDetailsServiceImpl.LoginUserDetails loginUser,
                              @RequestParam(value = "from", required = false, defaultValue = "") String from,
                              @RequestParam(value = "to", required = false, defaultValue = "") String to,
                              @RequestParam(value = "spUserId", required = false) Long spUserId,
                              @RequestParam(value = "p", required = false, defaultValue = "1") int p,
                              @RequestParam(value = "pz", required = false, defaultValue = "30") int pz) throws UnsupportedEncodingException {
        ModelAndView view = createModelAndView("index");
        /*此为4方，所以一下功能暂时去掉*/
        //判断角色，如果是管理员，如果是供号商，cp或者cp代理商
        /*if(loginUser.getLevel().getCode()>= UserLevel.AGENT.getCode()){
            Integer accountMoney = 0;
            if(loginUser.getLevel().getCode()==UserLevel.ADMIN_Hao.getCode()){
                spUserId = Long.valueOf(loginUser.getUsername()).longValue();
                //计算余额：充值-(订单总额-订单手续费)
                //这里查询手续费
                Integer chongzhiSum = cpAccountTradlogMapper.selectSumSpUserFeeChongzhi(spUserId);
                Order orderSum = orderMapper.selectSumSpUserFee(""+spUserId);
                accountMoney =chongzhiSum-(orderSum.getAmount()-orderSum.getAdvanceAmount());
                SpUser spUser  = spUserMapper.selectByPrimaryKey(spUserId.intValue());
                view.addObject("spUser", spUser);

            }
            if (StringUtils.isBlank(to) || StringUtils.isBlank(from)) {
                Calendar calendar = Calendar.getInstance();
                from = format.format(calendar.getTime())+" 00:00";
                calendar.add(Calendar.DAY_OF_YEAR, 1);
                to = format.format(calendar.getTime())+" 00:00";
            }
            p = p < 1 ? 1 : p;
            pz = pz <= 0 ? 10 : pz;
            String offset = String.valueOf((p - 1) * pz);
            String rows = String.valueOf(pz);

            List<CpAccountTradlog>lst = cpAccountTradlogMapper.list1(spUserId,from,to,offset,rows);
            CpAccountTradlog total = cpAccountTradlogMapper.total(spUserId,from,to);

            view.addObject("accountMoney", accountMoney);
            view.addObject("lst", lst);
            view.addObject("total", total);

            PageModel pageModel = new PageModel();
            pageModel.setPage(p);
            pageModel.setPageSize(pz);
            String url = "?";
            Map<String, String> params = new HashMap<>();

            if (spUserId!=null&&0!=spUserId) {
                url += String.format("&spUserId=%s", spUserId);
                params.put("spUserId", spUserId.toString());
            }
            if (StringUtils.isNotBlank(from)) {
                url += String.format("&from=%s", URLEncoder.encode(from, "utf8"));
                params.put("from", from.toString());
            }
            if (StringUtils.isNotBlank(to)) {
                url += String.format("&to=%s", URLEncoder.encode(to, "utf8"));
                params.put("to", to.toString());
            }
            if (!"?".equalsIgnoreCase(url)) {
                pageModel.setUrl(url);
            }
            pageModel.setTotal(total.getTotal());
            view.addObject("params", params);
            view.addObject("page", pageModel);
        }*/
        return view;
        //
    }

    @RequestMapping(value = {"/error/403"}, method = RequestMethod.GET)
    public ModelAndView error403() {

        return createModelAndView("error/403");
    }

    @RequestMapping(value = {"/error/404"}, method = RequestMethod.GET)
    public ModelAndView error404() {

        return createModelAndView("error/404");
    }

    @RequestMapping(value = {"/error/500"}, method = RequestMethod.GET)
    public ModelAndView error500() {

        return createModelAndView("error/500");
    }
}
